package com.cn.springcloudwechat.serviceImpl.articleAndMenu;


import com.cn.springcloudwechat.mapper.mapper.articleAndMenu.YmMenuTagMapper;
import com.cn.springcloudwechat.model.articleAndMenu.YmMenuTag;
import com.cn.springcloudwechat.model.articleAndMenu.vo.YmMenuTagVo;
import com.cn.springcloudwechat.service.articleAndMenu.YmMenuTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @program: YmMenuTagServiceImpl
 * @author: Administrator
 * @create: 2018-07-17 16:36
 */
@Service("ymMenuTagServiceImpl")
public class YmMenuTagServiceImpl implements YmMenuTagService {
    @Autowired
    YmMenuTagMapper ymMenuTagMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertYmMenuTag(YmMenuTag record) {
        return ymMenuTagMapper.insertSelective(record);
    }

    @Override
    @Cacheable
    public YmMenuTag getOneYmMenuTag(Long menuTagId) {
        return ymMenuTagMapper.selectByPrimaryKey(menuTagId);
    }

    @Override
    @CacheEvict(beforeInvocation = true)
    @Transactional(rollbackFor = Exception.class)
    public int updateYmMenuTag(YmMenuTag record) {
        return ymMenuTagMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<YmMenuTag> getAllMenuTag(YmMenuTag record) {
        return ymMenuTagMapper.getAllMenuTag(record);
    }

    @Override
    @Cacheable
    public List<YmMenuTag> getStairMenu() {
        return ymMenuTagMapper.getStairMenu();
    }

    @Override
    public List<YmMenuTagVo> getMenuTree(long menuTagId) {
        List<YmMenuTagVo> ymMenuTagVos = ymMenuTagMapper.getMenuTree(menuTagId);
        getMenuTreeSelect(ymMenuTagVos);
        return ymMenuTagVos;
    }

    private void getMenuTreeSelect(List<YmMenuTagVo> ymMenuTagVos) {
        if (ymMenuTagVos.size() > 0) {
            for (YmMenuTagVo ymMenuTagVo : ymMenuTagVos) {
                List<YmMenuTagVo> trees = ymMenuTagMapper.getMenuTree(ymMenuTagVo.getMenuTagId());
                ymMenuTagVo.setChildren(trees);
                getMenuTreeSelect(trees);
            }
        }
    }


}
